# -*- coding: utf-8 -*-

'''
@project : SmartHomeVideo
@FileName: ffmpeg_pipe
@Author  :linych 
@Time    :2021/2/27 15:44
@Desc  : 
'''
'''
import os

os.environ['http_proxy']='http://10.143.165.150:7001'
os.environ['https_proxy']='http://10.143.165.150:7001'
'''

import subprocess
import json
import numpy as np
import time
import ffmpeg




def video_decoder(out):
    s = time.time()
    
    p1 = subprocess.Popen(['ffprobe', '-show_format', '-show_streams','-loglevel','quiet', '-of', 'json', '-'], stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,bufsize=10**8)
    '''
    p2 = subprocess.Popen(
        ["ffmpeg", "-i", "-","-threads","6" ,"-vf", "select=eq(pict_type\,I)", "-vsync", "vfr", '-qscale:v', '2', '-pix_fmt', 'rgb24',
         '-vcodec', 'rawvideo','-loglevel','quiet', "-f", "image2pipe", "-"], close_fds=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE,bufsize=10**8)
    '''

    print('create sub {}'.format(time.time()-s))
    probe = p1.communicate(input=out)[0]
    print(probe)
    probe = json.loads(probe.decode('utf-8'))

    video_info = next(s for s in probe['streams'] if s['codec_type'] == 'video')
    width = int(video_info['width'])
    height = int(video_info['height'])
    sv = time.time()


    #video = p2.communicate(input=out)[0]

    video, err = (
        ffmpeg.input('http://10.143.165.25/tmp/20210127130801-20210127130807_ALARM.ps')
            .filter('fps', '1')
            .output('pipe:', format='rawvideo', pix_fmt='rgb24')
            .global_args('-loglevel', 'quiet')
            .run(capture_stdout=True)
    )
    print('p2 use : {}'.format(time.time()-sv))
    print('sub use : {}'.format(time.time()-s))
    video = np.frombuffer(video, np.uint8).reshape([-1, height, width, 3])

    print(video.shape)
    p1.terminate()
    #p2.terminate()

    return video


#
# def video_decoder(out):
#     s = time.time()
#     p1 = subprocess.Popen(['ffprobe', '-show_format', '-show_streams', '-loglevel', 'quiet', '-of', 'json', out],
#                           stdin=subprocess.PIPE,
#                           stdout=subprocess.PIPE, bufsize=10 ** 8)
#
#     p2 = subprocess.Popen(
#         ["ffmpeg", "-i", out, "-vf", "select=eq(pict_type\,I)", "-vsync", "vfr", '-qscale:v', '2', '-pix_fmt', 'rgb24',
#          '-vcodec', 'rawvideo', '-loglevel', 'quiet', "-f", "image2pipe", "-"], close_fds=True,
#         stdout=subprocess.PIPE, bufsize=10 ** 8)
#
#     print('create sub {}'.format(time.time() - s))
#     probe = p1.communicate()[0]
#     probe = json.loads(probe.decode('utf-8'))
#
#     video_info = next(s for s in probe['streams'] if s['codec_type'] == 'video')
#     width = int(video_info['width'])
#     height = int(video_info['height'])
#     sv = time.time()
#     video = p2.communicate()[0]
#     print('p2 use : {}'.format(time.time() - sv))
#     print('sub use : {}'.format(time.time() - s))
#     video = np.frombuffer(video, np.uint8).reshape([-1, height, width, 3])
#
#     print(video.shape)
#     p1.terminate()
#     p2.terminate()
#
#     return video

